using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using Microsoft.AnalysisServices.AdomdClient;
using System.Windows.Forms;
using System.ServiceProcess;
namespace SetupHelper
{
    [RunInstaller(true)]
    public partial class InstallerSupport : Installer
    {
        public InstallerSupport()
        {
            InitializeComponent();
        }

        private void InstallerSupport_AfterInstall(object sender, InstallEventArgs e)
        {
            // Register the server
            if (!Context.Parameters.ContainsKey("Server"))
            {
                return;
            }

            string server = Context.Parameters["Server"];
            if (server == null || server.Length == 0)
            {
                // Nothing to do
                return;
            }
            server = server.Trim();

            if (RegisterPlugin(server))
            {
                RestartService(server);
            }

        }

        private bool RegisterPlugin(string server)
        {
            AdomdConnection cn = new AdomdConnection();
            cn.ConnectionString = string.Format("Data Source={0}", server);
            try
            {
                cn.Open();
                string commandTemplate = Resources.DeploymentScript;

                string commandText = string.Format(
                                        commandTemplate,
                                        "Avanade_Support_Vector_Machine",
                                        "F9B1928C-53CC-11DC-92D2-51C855D89593");

                AdomdCommand cmd = new AdomdCommand();
                cmd.Connection = cn;
                cmd.CommandText = commandText;
                cmd.ExecuteNonQuery();
                cn.Close();
            }
            catch (System.Exception ex)
            {

                string title = string.Format(
                           Resources.ServerError,
                            server);
                string text = Resources.ManualRegistration + "\r\n" + ex.Message;

                MessageBox.Show(text, title);

                Context.LogMessage(title);
                Context.LogMessage(ex.Message);
                return false;
            }
            return true;
        }

        private bool RestartService(string server)
        {
            try
            {
                string instanceName = string.Empty;
                string serviceName = "MSSQLServerOLAPService";
                int bsIndex = server.IndexOf('\\');
                if ( bsIndex > 0)
                {
                    instanceName = server.Substring(bsIndex + 1);
                    serviceName = string.Format("MSOLAP${0}", instanceName);
                }
                ServiceController[] svcs = ServiceController.GetServices();
                for (int nIndex = 0; nIndex < svcs.Length; nIndex++)
                {
                    ServiceController sc = svcs[nIndex];
                    if (0 == string.Compare(sc.ServiceName, serviceName, true))
                    {
                        // stop then restart the service
                        if( sc.Status == ServiceControllerStatus.Running )
                        {
                            sc.Stop();
                            // Wait 5 seconds for the service to stop
                            System.Threading.Thread.Sleep(5000);
                        }
                        // try to restart the service
                        sc.Start();
                    }
                }

            }
            catch
            {
            }
            return true;
        }

    }
}